home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 10Y9B0N (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  7.7 KB  |  305 lines

  1. package com.sun.java.swing.border;
  2.  
  3. import com.sun.java.swing.UIManager;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Dimension;
  7. import java.awt.Font;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.Insets;
  11. import java.awt.Point;
  12. import java.awt.Rectangle;
  13.  
  14. public class TitledBorder extends AbstractBorder {
  15.    protected String title;
  16.    protected Border border;
  17.    protected int titlePosition;
  18.    protected int titleJustification;
  19.    protected Font titleFont;
  20.    protected Color titleColor;
  21.    public static final int DEFAULT_POSITION = 0;
  22.    public static final int ABOVE_TOP = 1;
  23.    public static final int TOP = 2;
  24.    public static final int BELOW_TOP = 3;
  25.    public static final int ABOVE_BOTTOM = 4;
  26.    public static final int BOTTOM = 5;
  27.    public static final int BELOW_BOTTOM = 6;
  28.    public static final int DEFAULT_JUSTIFICATION = 0;
  29.    public static final int LEFT = 1;
  30.    public static final int CENTER = 2;
  31.    public static final int RIGHT = 3;
  32.    protected static final int EDGE_SPACING = 2;
  33.    protected static final int TEXT_SPACING = 2;
  34.    protected static final int TEXT_INSET_H = 5;
  35.  
  36.    public TitledBorder(Border border) {
  37.       this(border, "", 1, 2, (Font)null, (Color)null);
  38.    }
  39.  
  40.    public TitledBorder(Border border, String title) {
  41.       this(border, title, 1, 2, (Font)null, (Color)null);
  42.    }
  43.  
  44.    public TitledBorder(Border border, String title, int titleJustification, int titlePosition) {
  45.       this(border, title, titleJustification, titlePosition, (Font)null, (Color)null);
  46.    }
  47.  
  48.    public TitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont) {
  49.       this(border, title, titleJustification, titlePosition, titleFont, (Color)null);
  50.    }
  51.  
  52.    public TitledBorder(Border border, String title, int titleJustification, int titlePosition, Font titleFont, Color titleColor) {
  53.       this.title = title;
  54.       this.border = border;
  55.       this.titleFont = titleFont;
  56.       this.titleColor = titleColor;
  57.       this.setTitleJustification(titleJustification);
  58.       this.setTitlePosition(titlePosition);
  59.    }
  60.  
  61.    public TitledBorder(String title) {
  62.       this((Border)null, title, 1, 2, (Font)null, (Color)null);
  63.    }
  64.  
  65.    public Border getBorder() {
  66.       Border b = this.border;
  67.       if (b == null) {
  68.          b = UIManager.getBorder("TitledBorder.border");
  69.       }
  70.  
  71.       return b;
  72.    }
  73.  
  74.    public Insets getBorderInsets(Component c) {
  75.       int descent = 0;
  76.       int ascent = 16;
  77.       Insets retval;
  78.       if (this.getBorder() != null) {
  79.          retval = this.getBorder().getBorderInsets(c);
  80.       } else {
  81.          retval = new Insets(0, 0, 0, 0);
  82.       }
  83.  
  84.       retval.left += 4;
  85.       retval.right += 4;
  86.       retval.top += 4;
  87.       retval.bottom += 4;
  88.       if (c != null && this.getTitle() != null && !this.getTitle().equals("")) {
  89.          Font font = this.getFont(c);
  90.          FontMetrics fm = c.getFontMetrics(font);
  91.          if (fm != null) {
  92.             descent = fm.getDescent();
  93.             ascent = fm.getAscent();
  94.          }
  95.  
  96.          switch (this.getTitlePosition()) {
  97.             case 0:
  98.             case 2:
  99.                retval.top += ascent + descent;
  100.                break;
  101.             case 1:
  102.                retval.top += ascent + descent + (Math.max(2, 4) - 2);
  103.                break;
  104.             case 3:
  105.                retval.top += ascent + descent + 2;
  106.                break;
  107.             case 4:
  108.                retval.bottom += ascent + descent + 2;
  109.                break;
  110.             case 5:
  111.                retval.bottom += ascent + descent;
  112.                break;
  113.             case 6:
  114.                retval.bottom += ascent + 2;
  115.          }
  116.  
  117.          return retval;
  118.       } else {
  119.          return retval;
  120.       }
  121.    }
  122.  
  123.    protected Font getFont(Component c) {
  124.       Font font;
  125.       if ((font = this.getTitleFont()) != null) {
  126.          return font;
  127.       } else {
  128.          return c != null && (font = c.getFont()) != null ? font : new Font("Dialog", 0, 12);
  129.       }
  130.    }
  131.  
  132.    public Dimension getMinimumSize(Component c) {
  133.       Insets insets = this.getBorderInsets(c);
  134.       Dimension minSize = new Dimension(insets.right + insets.left, insets.top + insets.bottom);
  135.       Font font = this.getFont(c);
  136.       FontMetrics fm = c.getFontMetrics(font);
  137.       switch (this.titlePosition) {
  138.          case 0:
  139.          case 2:
  140.          case 3:
  141.          case 4:
  142.          case 5:
  143.          default:
  144.             minSize.width += fm.stringWidth(this.getTitle());
  145.             break;
  146.          case 1:
  147.          case 6:
  148.             minSize.width = Math.max(fm.stringWidth(this.getTitle()), minSize.width);
  149.       }
  150.  
  151.       return minSize;
  152.    }
  153.  
  154.    public String getTitle() {
  155.       return this.title;
  156.    }
  157.  
  158.    public Color getTitleColor() {
  159.       Color c = this.titleColor;
  160.       if (c == null) {
  161.          c = UIManager.getColor("TitledBorder.titleColor");
  162.       }
  163.  
  164.       return c;
  165.    }
  166.  
  167.    public Font getTitleFont() {
  168.       Font f = this.titleFont;
  169.       if (f == null) {
  170.          f = UIManager.getFont("TitledBorder.font");
  171.       }
  172.  
  173.       return f;
  174.    }
  175.  
  176.    public int getTitleJustification() {
  177.       return this.titleJustification;
  178.    }
  179.  
  180.    public int getTitlePosition() {
  181.       return this.titlePosition;
  182.    }
  183.  
  184.    public boolean isBorderOpaque() {
  185.       return false;
  186.    }
  187.  
  188.    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  189.       if (this.getTitle() != null && !this.getTitle().equals("")) {
  190.          Rectangle grooveRect = new Rectangle(2, 2, width - 4, height - 4);
  191.          Font font = g.getFont();
  192.          Color color = g.getColor();
  193.          g.setFont(this.getFont(c));
  194.          FontMetrics fm = g.getFontMetrics();
  195.          int fontHeight = fm.getHeight();
  196.          int descent = fm.getDescent();
  197.          int ascent = fm.getAscent();
  198.          Point textLoc = new Point();
  199.          int stringWidth = fm.stringWidth(this.getTitle());
  200.          Insets insets;
  201.          if (this.getBorder() != null) {
  202.             insets = this.getBorder().getBorderInsets(c);
  203.          } else {
  204.             insets = new Insets(0, 0, 0, 0);
  205.          }
  206.  
  207.          switch (this.getTitlePosition()) {
  208.             case 0:
  209.             case 2:
  210.                int var18 = Math.max(0, ascent / 2 + 2 - 2);
  211.                grooveRect.y += var18;
  212.                grooveRect.height -= var18;
  213.                textLoc.y = grooveRect.y - descent + (insets.top + ascent + descent) / 2;
  214.                break;
  215.             case 1:
  216.                int diff = ascent + descent + (Math.max(2, 4) - 2);
  217.                grooveRect.y += diff;
  218.                grooveRect.height -= diff;
  219.                textLoc.y = grooveRect.y - (descent + 2);
  220.                break;
  221.             case 3:
  222.                textLoc.y = grooveRect.y + insets.top + ascent + 2;
  223.                break;
  224.             case 4:
  225.                textLoc.y = grooveRect.y + grooveRect.height - (insets.bottom + descent + 2);
  226.                break;
  227.             case 5:
  228.                grooveRect.height -= fontHeight / 2;
  229.                textLoc.y = grooveRect.y + grooveRect.height - descent + (ascent + descent - insets.bottom) / 2;
  230.                break;
  231.             case 6:
  232.                grooveRect.height -= fontHeight;
  233.                textLoc.y = grooveRect.y + grooveRect.height + ascent + 2;
  234.          }
  235.  
  236.          this.getBorder().paintBorder(c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height);
  237.          switch (this.getTitleJustification()) {
  238.             case 0:
  239.             case 1:
  240.                textLoc.x = grooveRect.x + 5 + insets.left;
  241.                break;
  242.             case 2:
  243.                textLoc.x = grooveRect.x + (grooveRect.width - stringWidth) / 2;
  244.                break;
  245.             case 3:
  246.                textLoc.x = grooveRect.x + grooveRect.width - (stringWidth + 5 + insets.right);
  247.          }
  248.  
  249.          g.setColor(c.getBackground());
  250.          g.fillRect(textLoc.x - 2, textLoc.y - (fontHeight - descent), stringWidth + 4, fontHeight - descent);
  251.          g.setColor(this.getTitleColor());
  252.          g.drawString(this.getTitle(), textLoc.x, textLoc.y);
  253.          g.setFont(font);
  254.          g.setColor(color);
  255.       } else {
  256.          this.getBorder().paintBorder(c, g, x, y, width, height);
  257.       }
  258.    }
  259.  
  260.    public void setBorder(Border border) {
  261.       this.border = border;
  262.    }
  263.  
  264.    public void setTitle(String title) {
  265.       this.title = title;
  266.    }
  267.  
  268.    public void setTitleColor(Color titleColor) {
  269.       this.titleColor = titleColor;
  270.    }
  271.  
  272.    public void setTitleFont(Font titleFont) {
  273.       this.titleFont = titleFont;
  274.    }
  275.  
  276.    public void setTitleJustification(int titleJustification) {
  277.       switch (titleJustification) {
  278.          case 0:
  279.          case 1:
  280.          case 2:
  281.          case 3:
  282.             this.titleJustification = titleJustification;
  283.             return;
  284.          default:
  285.             throw new IllegalArgumentException(titleJustification + " is not a valid title justification.");
  286.       }
  287.    }
  288.  
  289.    public void setTitlePosition(int titlePosition) {
  290.       switch (titlePosition) {
  291.          case 0:
  292.          case 1:
  293.          case 2:
  294.          case 3:
  295.          case 4:
  296.          case 5:
  297.          case 6:
  298.             this.titlePosition = titlePosition;
  299.             return;
  300.          default:
  301.             throw new IllegalArgumentException(titlePosition + " is not a valid title position.");
  302.       }
  303.    }
  304. }
  305.